package ArraySort;

public class InsertSort
{
    public static int[] insertSort(int[] anArray)
    {
        int[] temp=new int[anArray.length];
//        for(int i = 0; i < anArray.length; i++)
//        {
//            temp[i]=0;
//        }
        //  System.out.println("length is "+anArray.length);
        for(int index = 0; index < anArray.length; index++)
        {
            int judge = 0;
            for(int j = 0; j < temp.length; j++)
            {
                if(anArray[index] < temp[j])
                {
                    for(int k = temp.length - 2; k >= j; k--)
                    {
                        temp[k + 1] = temp[k];
                    }
                    judge = 1;
                }
                if(judge == 1)
                {
                    temp[j] = anArray[index];
                    break;
                }
            }
            if(judge == 0)
            {
                temp[index] = anArray[index];
            }
        }
        //循环插入，若比前一个大则后移数组元素
        return temp;
    }
}
